package com.prdiot.poetry.service;

import com.prdiot.poetry.dao.AuthorRepository;
import com.prdiot.poetry.dao.MingjuRepository;
import com.prdiot.poetry.domains.Author;
import com.prdiot.poetry.domains.Mingju;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class AuthorService {

    @Autowired
    AuthorRepository authorRepository;

    public List<Author> getTopAuthors(int nums) {
        return authorRepository.getTopAuthors(nums);
    }

    public String getAuthorNameByAuthorId(String authorId) {
        Optional<Author> authorOptional = authorRepository.findByAuthorId(authorId);
        if (authorOptional.isPresent()){
            return authorOptional.get().getAuthorName();
        }
        return "";
    }

    public Author findByAuthorId(String authorId) {
        Optional<Author> authorOptional = authorRepository.findByAuthorId(authorId);
        if (authorOptional.isPresent()){
            return authorOptional.get();
        }
        return null;
    }

    public Page<Author> getAllAuthors(int curPage){
        Sort sort = new Sort(Sort.Direction.DESC, "poemNum");
        Pageable pageable = new PageRequest(curPage - 1, 10, sort); // （当前页， 每页记录数， 排序方式）
        Page<Author> authorPage = authorRepository.findAll(pageable);
        return  authorPage;
    }

}
